package com.action;

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import com.model.Customer;
import com.model.Product;
import com.service.FileService;
import com.service.impl.FileServiceImpl;

@WebServlet("/FileController")
@MultipartConfig
public class FileController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public FileController() {
		super();
		// TODO Auto-generated constructor stub
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String method = request.getParameter("method");
		if ("customerXls".equals(method)) {
			addCustomerXls(request, response);
		} else if ("productXls".equals(method)) {
			addProductXls(request, response);
		} else {
			System.out.println(FileController.class + "请求不合法！");
		}
	}

	protected void addCustomerXls(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String savePath = request.getServletContext().getRealPath("/upload");

		Part part = request.getPart("file");
		File file = new File(savePath + File.separator);
		if (!file.exists()) {
			file.mkdirs();
		}
		String header = part.getHeader("content-disposition");
		String fileName = getFileName(header);
		part.write(savePath + File.separator + fileName);
		File file2 = new File(savePath + File.separator + fileName);
		if (file2.exists()) {
			FileService fileService = new FileServiceImpl();
			Customer customer = new Customer();
			boolean b = fileService.saveCustomer(savePath + File.separator
					+ fileName, customer);
			if (b) {
				response.getWriter().write("{\"success\":true,\"msg\":\"成功\"}");
				response.getWriter().close();
			} else {
				response.getWriter().write(
						"{\"success\":false,\"msg\":\"已存在记录\"}");
				response.getWriter().close();
			}
		}
	}

	protected void addProductXls(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String savePath = request.getServletContext().getRealPath("/upload");

		Part part = request.getPart("file");
		File file = new File(savePath + File.separator);
		if (!file.exists()) {
			file.mkdirs();
		}
		String header = part.getHeader("content-disposition");
		String fileName = getFileName(header);
		part.write(savePath + File.separator + fileName);
		File file2 = new File(savePath + File.separator + fileName);
		if (file2.exists()) {
			FileService fileService = new FileServiceImpl();
			Product product = new Product();
			boolean b = fileService.saveProduct(savePath + File.separator
					+ fileName, product);
			if (b) {
				response.getWriter().write("{\"success\":true,\"msg\":\"成功\"}");
				response.getWriter().close();
			} else {
				response.getWriter().write(
						"{\"success\":false,\"msg\":\"已存在记录\"}");
				response.getWriter().close();
			}
		}
	}

	public String getFileName(String header) {
		String[] tempArr1 = header.split(";");
		String[] tempArr2 = tempArr1[2].split("=");
		// 获取文件名，兼容各种浏览器的写法
		String fileName = tempArr2[1].substring(
				tempArr2[1].lastIndexOf(File.separator) + 1).replaceAll("\"",
				"");
		// fileName = UUID.randomUUID().toString() + fileName;
		return fileName;
	}
}
